package com.example.demo.controller;

import com.example.demo.entity.GoodsInfo;
import com.example.demo.entity.User;
import com.example.demo.repository.GoodsRepository;
import com.example.demo.service.IUserService;
import com.example.demo.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * Created by yujin.liu on 2018/8/7.
 */
@RestController
@RequestMapping("/api/test")
@Api(value="TestController",description="测试接口")
public class TestController {

    @Autowired
    private IUserService userService;

    @Autowired
    private GoodsRepository goodsRepository;

    @Resource(name = "redisUtil")
    private RedisUtil redisUtil;


    @ApiOperation(value="根据用户id获取用户")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
    @RequestMapping(value = "/getUserById", method = {RequestMethod.POST,RequestMethod.GET})
    public User getUserById(@RequestParam(value = "id") Long id){

        User user = userService.getUserById(id);
        return user;
    }

    @ApiOperation(value="从 redis 获取值")
    @ApiImplicitParam(name = "value", value = "redis 值", required = true, dataType = "String")
    @RequestMapping(value = "/getValueFromRedis", method = {RequestMethod.POST,RequestMethod.GET})
    public String getValueFromRedis(@RequestParam(value = "value") String value){

        redisUtil.set("key", value);

        return (String)redisUtil.get("key");
    }

    @ApiOperation(value="es 保存")
    @RequestMapping(value = "/saveEs", method = {RequestMethod.POST,RequestMethod.GET})
    public String saveEs(){
        GoodsInfo goodsInfo = new GoodsInfo(System.currentTimeMillis(),"商品" + System.currentTimeMillis(),"这是一个测试商品");
        goodsRepository.save(goodsInfo);
        return "success";
    }

    @ApiOperation(value="es 取值")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long")
    @RequestMapping(value = "/getEs", method = {RequestMethod.POST,RequestMethod.GET})
    public Optional<GoodsInfo> getEs(@RequestParam(value = "id") Long id){

        Optional<GoodsInfo> goodsInfo = goodsRepository.findById(id);

        return goodsInfo;
    }
}
